package com.gbb.client;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gbb.ipc.IMyAidlInterface;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    private IMyAidlInterface aidlInterface;

    private TextView msgView;
    private StringBuffer sBuffer = new StringBuffer();

    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name,IBinder service){
            sBuffer.delete(0,sBuffer.length());
            sBuffer.append("ComponentName=").append(name.getClassName()).append("\n");
            sBuffer.append("IBinder=").append(service).append("\n");
            aidlInterface = IMyAidlInterface.Stub.asInterface(service);
            sBuffer.append("IMyAidlInterface=").append(aidlInterface);

            Log.d(TAG,sBuffer.toString());
            msgView.setText(sBuffer);
        }

        @Override
        public void onServiceDisconnected(ComponentName name){

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_bind_remote).setOnClickListener(this);
        findViewById(R.id.btn_bind_local).setOnClickListener(this);
        findViewById(R.id.btn_exe).setOnClickListener(this);
        findViewById(R.id.btn_unbind).setOnClickListener(this);

        msgView = (TextView) findViewById(R.id.tx_messageShow);
    }

    @Override
    public void onClick(View v){

        switch(v.getId()) {

            case R.id.btn_bind_remote: {
                Intent mIntent = new Intent();
                mIntent.setPackage("com.gbb.service");
                boolean isSuccess = bindService(mIntent,conn,Context.BIND_AUTO_CREATE);
                Log.d(TAG,isSuccess + "");
            }
            break;

            case R.id.btn_bind_local: {
                Intent mIntent = new Intent();
                mIntent.setPackage("com.gbb.client");
                boolean isSuccess = bindService(mIntent,conn,Context.BIND_AUTO_CREATE);
                Log.d(TAG,isSuccess + "");
            }
            break;

            case R.id.btn_unbind:
                unbindService(conn);
                sBuffer.delete(0,sBuffer.length());
                msgView.setText(sBuffer);
                break;

            case R.id.btn_exe:
                if(aidlInterface == null) {
                    Toast.makeText(this,"远程服务未启动",Toast.LENGTH_SHORT).show();
                    return;
                }

                try {
                    String result = aidlInterface.hello("张三");
                    sBuffer.append("\n =>").append(result);
                    Log.d(TAG,result);
                    msgView.setText(sBuffer);
                } catch(RemoteException e) {
                    e.printStackTrace();
                }
                break;

        }

    }
}
